package com.mdxq.mall.product.service.impl;

import com.mdxq.mall.product.entity.Brand;
import com.mdxq.mall.product.mapper.BrandMapper;
import com.mdxq.mall.product.service.BrandService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * 品牌表 服务层实现。
 *
 * @author ShuJie
 * @since 2025-01-12
 */
@Service
@Slf4j
// 方法在(Service)类之前, 提供当前类下所有方法统一的缓存设置(包括缓存管理器)
@CacheConfig(cacheNames = "brandCache",cacheManager = "cacheManager")
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>  implements BrandService{
    @Override
    // @Cacheable 先判断缓存里有没有,如果有就不执行方法,直接从缓存里取,如果没有,,执行方法,将返回值存入缓存
    @Cacheable(value = "brandCache",key = "#id")
    // @CachePut 无论缓存中有没有,都执行这个方法,然后将执行结果放入缓存
    //@CachePut(value = "brandCache",key="#id")
    public Brand getById(Serializable id) {
        log.info("从Mysql查询到数据");
        return super.getById(id);
    }

    @Override
//    @CacheEvict(value = "brandCache",key ="#entity.id")
    @CacheEvict(value = "brandCache",allEntries = true)
    public boolean updateById(Brand entity) {
        return super.updateById(entity);
    }

    @Override
    @Transactional
    @CacheEvict(value = "brandCache",allEntries = true)
    public boolean updateShowStatus(String ids, Integer showStatus) {
        for(String id:ids.split(",")){
            Brand brand=mapper.selectOneById(Long.valueOf(id));
            brand.setShowStatus(showStatus);
            mapper.update(brand);
        }
        return true;
    }

    @Override
    @CacheEvict(value = "brandCache",allEntries = true)
    public boolean updateFactoryStatus(String ids, Integer factoryStatus) {
        for(String id:ids.split(",")){
            Brand brand=mapper.selectOneById(Long.valueOf(id));
            brand.setFactoryStatus(factoryStatus);
            mapper.update(brand);
        }
        return true;
    }
}
